package com.muyu.vehicle.config;

import com.muyu.vehicle.domain.VehicleFaultInfo;
import com.muyu.vehicle.process.*;
import com.muyu.vehicle.service.*;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 牧鱼
 * @Classname RunAfter
 * @Description TODO
 * @Date 2021/9/15
 */
@Component
public class RunAfter implements ApplicationRunner {

    /**
     * redis
     */
    private final RedisTemplate<String,String> redisTemplate;

    /**
     * 故障信息
     */
    private final IVehicleFaultInfoService vehicleFaultInfoService;
    /**
     * 车辆信息
     */
    private final IVehicleInfoService vehicleInfoService;
    /**
     * 故障记录
     */
    private final IVehicleFaultLogService vehicleFaultLogService;
    /**
     * 电子围栏
     */
    private final IVehicleFenceService vehicleFenceService;

    /**
     * 车辆行驶记录
     */
    private final IVehicleMoveInfoService vehicleMoveInfoService;


    public RunAfter(IVehicleFaultInfoService vehicleFaultInfoService,
                    RedisTemplate<String, String> redisTemplate,
                    IVehicleInfoService vehicleInfoService,
                    IVehicleFaultLogService vehicleFaultLogService,
                    IVehicleFenceService vehicleFenceService,
                    IVehicleMoveInfoService vehicleMoveInfoService) {
        this.vehicleFaultInfoService = vehicleFaultInfoService;
        this.redisTemplate = redisTemplate;
        this.vehicleInfoService = vehicleInfoService;
        this.vehicleFaultLogService = vehicleFaultLogService;
        this.vehicleFenceService = vehicleFenceService;
        this.vehicleMoveInfoService = vehicleMoveInfoService;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 车辆下线线程
        ThreadPool.scheduleAtFixedRate(
                new ProcessVehicleOffLineThread(redisTemplate, vehicleInfoService, vehicleMoveInfoService),
                1,1, TimeUnit.SECONDS);
        // 车辆上线线程
        ThreadPool.scheduleAtFixedRate(
                new ProcessVehicleOnLineThread(redisTemplate,vehicleInfoService, vehicleMoveInfoService),
                1,1, TimeUnit.SECONDS);
        // 车辆故障接受
        ThreadPool.scheduleAtFixedRate(
                new ParsingVehicleLogThread(redisTemplate,vehicleFaultLogService),
                1,1,TimeUnit.SECONDS
        );

        // 系统启动更新车辆与电子围栏
        synchronizeVehicleFenceBind();
        // 系统启动更新电子围栏信息
        synchronizeFenceInfo();
        List<VehicleFaultInfo> vehicleFaultInfos = vehicleFaultInfoService.selectVehicleFaultInfoList(new VehicleFaultInfo());

        HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
        vehicleFaultInfos.forEach( vehicleFaultInfo -> {
            hashOperations.put(vehicleFaultInfo.getFaultGroup(),
                    vehicleFaultInfo.getFaultSite() + ":" + vehicleFaultInfo.getFaultValue() ,
                    vehicleFaultInfo.getFaultLabel());
        });

    }

    /**
     * 同步车辆绑定电子围栏信息
     */
    public void synchronizeVehicleFenceBind(){
        ThreadPool.exeThread(new SynchronizeVehicleFenceThread(redisTemplate,vehicleInfoService,null));
    }

    /**
     * 电子围栏同步
     */
    public void synchronizeFenceInfo(){
        ThreadPool.exeThread(new SynchronizeFenceInfoThread(redisTemplate , vehicleFenceService , null));
    }
}
